<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class ChpassStudentForm extends CFormModel {

    public $old_password;
    public $new_password;
    public $re_new_password;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            // username and password are required
            array('old_password, new_password, re_new_password', 'required'),
            array('re_new_password', 'compare', 'compareAttribute' => 'new_password'),
            array('old_password','isvalidpassword'),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'old_password' => 'Password Lama',
            'new_password' => 'Password Baru',
            're_new_password' => 'Ulangi Password Baru',
        );
    }

    /**
     * Authenticates the password.
     * This is the 'authenticate' validator as declared in rules().
     */
    public function isvalidpassword($attribute, $params) {
        if (!$this->hasErrors()) {
            $model = Pelajar::model()->findByPk(Yii::app()->session['student']->id_pelajar);
            if(md5($this->old_password) != $model->password){
                $this->addError('old_password', 'Password lama tidak cocok');
            }
                
        }
    }
    
    public function chpassword(){
        Pelajar::model()->updateByPk(Yii::app()->session['student']->id_pelajar, array(
            'password'=>  md5($this->new_password),
        ));
    }
}
